{% extends 'base.html' %}

{% block content %}
    <div class="row mb-4">
        <div class="col-md-6">
            <h1>问题列表</h1>
        </div>
        <div class="col-md-6 text-right">
            <a href="{{ url_for('main.create_issue') }}" class="btn btn-primary">创建新问题</a>
        </div>
    </div>

    <div class="card mb-4">
        <div class="card-header">
            <h5>过滤和搜索</h5>
        </div>
        <div class="card-body">
            <form method="get" action="{{ url_for('main.list_issues') }}">
                <div class="form-row">
                    <div class="form-group col-md-3">
                        <label for="status">状态</label>
                        <select class="form-control" id="status" name="status">
                            <option value="all">全部</option>
                            <option value="open" {% if request.args.get('status') == 'open' %}selected{% endif %}>未解决</option>
                            <option value="closed" {% if request.args.get('status') == 'closed' %}selected{% endif %}>已解决</option>
                        </select>
                    </div>
                    <div class="form-group col-md-3">
                        <label for="priority">优先级</label>
                        <select class="form-control" id="priority" name="priority">
                            <option value="all">全部</option>
                            <option value="low" {% if request.args.get('priority') == 'low' %}selected{% endif %}>低</option>
                            <option value="medium" {% if request.args.get('priority') == 'medium' %}selected{% endif %}>中</option>
                            <option value="high" {% if request.args.get('priority') == 'high' %}selected{% endif %}>高</option>
                            <option value="urgent" {% if request.args.get('priority') == 'urgent' %}selected{% endif %}>紧急</option>
                        </select>
                    </div>
                    <div class="form-group col-md-3">
                        <label for="tag">标签</label>
                        <select class="form-control" id="tag" name="tag">
                            <option value="all">全部</option>
                            {% for tag in all_tags %}
                            <option value="{{ tag.name }}" {% if request.args.get('tag') == tag.name %}selected{% endif %}>{{ tag.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="form-group col-md-3">
                        <label for="milestone">里程碑</label>
                        <select class="form-control" id="milestone" name="milestone">
                            <option value="all">全部</option>
                            {% for milestone in all_milestones %}
                            <option value="{{ milestone.id }}" {% if request.args.get('milestone') == milestone.id|string %}selected{% endif %}>{{ milestone.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
                <div class="form-row">
                    <div class="form-group col-md-3">
                        <label for="assignee">负责人</label>
                        <select class="form-control" id="assignee" name="assignee">
                            <option value="all">全部</option>
                            {% for user in all_users %}
                            <option value="{{ user.id }}" {% if request.args.get('assignee') == user.id|string %}selected{% endif %}>{{ user.username }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="search">搜索</label>
                        <input type="text" class="form-control" id="search" name="search" placeholder="搜索问题标题或描述" value="{{ request.args.get('search', '') }}">
                    </div>
                    <div class="form-group col-md-3 align-self-end">
                        <button type="submit" class="btn btn-primary btn-block">应用过滤</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    {% if issues %}
    <div class="table-responsive">
        <table class="table table-striped table-hover">
            <thead class="thead-dark">
                <tr>
                    <th>编号</th>
                    <th>标题</th>
                    <th>状态</th>
                    <th>优先级</th>
                    <th>负责人</th>
                    <th>创建时间</th>
                    <th>更新时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                {% for issue in issues %}
                <tr>
                    <td>#{{ issue.id }}</td>
                    <td><a href="{{ url_for('main.view_issue', issue_id=issue.id) }}">{{ issue.title }}</a></td>
                    <td>
                        <span class="badge {% if issue.status == 'open' %}badge-danger{% else %}badge-success{% endif %}">
                            {{ issue.status }}
                        </span>
                    </td>
                    <td>
                        <span class="badge {% if issue.priority == 'urgent' %}badge-danger{% elif issue.priority == 'high' %}badge-warning{% elif issue.priority == 'medium' %}badge-info{% else %}badge-secondary{% endif %}">
                            {{ issue.priority }}
                        </span>
                    </td>
                    <td>{{ issue.assignee.username if issue.assignee else '未分配' }}</td>
                    <td>{{ issue.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                    <td>{{ issue.updated_at.strftime('%Y-%m-%d %H:%M') }}</td>
                    <td>
                        <a href="{{ url_for('main.view_issue', issue_id=issue.id) }}" class="btn btn-sm btn-info">查看</a>
                        <a href="{{ url_for('main.edit_issue', issue_id=issue.id) }}" class="btn btn-sm btn-primary">编辑</a>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    {% else %}
    <div class="alert alert-info">
        没有找到匹配的问题
    </div>
    {% endif %}
{% endblock %}